package com.mystudy.leetcode.problem.tree.p_654;

import com.mystudy.leetcode.base.TreeNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @program: infoalgorithm
 * @description: 最大二叉树
 * @author: zhouzhilong
 * @create: 2019-07-26 14:51
 **/
public class Solution {
    /**
     * logger
     */
    private static final Logger LOGGER = LoggerFactory.getLogger(Solution.class);

    /**
     *
     * @param nums
     * @return
     */
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        TreeNode root =  constructMaximum(nums,0,nums.length);
        return root;
    }

    public TreeNode constructMaximum(int []nums,int start,int end){
        if(start == end){
            return null;
        }
        int max = Integer.MIN_VALUE;
        int index = 0;
        for (int i = start; i < end;i++) {
            if(nums[i]>max){
                max = nums[i];
                index = i;
            }
        }


        TreeNode root = new TreeNode(max);
        root.left = constructMaximum(nums,start,index);
        root.right = constructMaximum(nums,index+1,end);
        return root;
    }






}
